# 导入pymysql模块
import pymysql

# 与数据库建立连接
conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='admin',
    database='mumu',
    charset='utf8',
    cursorclass=pymysql.cursors.DictCursor
)

# 执行sql语句
sql = "select * from user"

# 实例化一个游标对象
cursor = conn.cursor() # cursor=pymysql.cursors.DictCursor
cursor.execute(sql)

result = cursor.fetchall()
# print(result)

print(result)
for row in result:
    print(row['user_name'])

# 关闭游标与数据库连接
cursor.close()
conn.close()